package com.course.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.course.domain.pojo.CourseDetail;
import com.course.domain.pojo.Media;
import com.course.domain.vo.TreeCourseDetailVo;
import com.course.feign.MediaFeignClient;
import com.course.mapper.CourseDetailMapper;
import com.course.service.ICourseDetailService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小杰大人
 * @since 2025-02-22
 */
@Service
public class CourseDetailServiceImpl extends ServiceImpl<CourseDetailMapper, CourseDetail> implements ICourseDetailService {

    @Resource
    private MediaFeignClient mediaFeignClient;

    @Override
    public List<TreeCourseDetailVo> getCourseContentList(Long courseId) {
        List<CourseDetail> list = baseMapper.getCourseDirectoryList(courseId);
        // 1: 构建父子关系返回 TreeMap 确保一级内容也是有序的
        Map<Long, TreeCourseDetailVo> map = new TreeMap<>();
        // 2: 获取所有关联的文件信息
        Set<Long> mediaIds = new HashSet<>();
        // 3: 构建文件父子结构
        for (CourseDetail courseDetail : list) {
            // 3.1：如果存在文件信息
            Optional.ofNullable(courseDetail.getMediaId()).ifPresent(mediaIds::add);
            // 3.2: 转为增强类
            TreeCourseDetailVo detailsDto = TreeCourseDetailVo.createTreeCourseDetailsDto(courseDetail);
            // 3.3: 判断是否为一级内容
            if (Objects.isNull(courseDetail.getParentId())){
                map.put(courseDetail.getId(),detailsDto);
            }else {
                // 3.4: 二级内容 子内容 添加进父级中
                map.get(courseDetail.getParentId()).getChildren().add(detailsDto);
            }
        }
        // 4: 获取课程内容有关的文件信息
        Map<Long, Media> mediaMap = mediaFeignClient.querySelectMediaByIds(new ArrayList<>(mediaIds)).getData();
        List<TreeCourseDetailVo> result = new ArrayList<>(map.values());
        // 5: 添加文件信息
        for (TreeCourseDetailVo treeCourseDetailsVo : result) {
            // 一级内容无文件
            for (TreeCourseDetailVo child : treeCourseDetailsVo.getChildren()) {
                Media media = mediaMap.get(child.getMediaId());
                if (media == null){
                    continue;
                }
                // 设置文件访问路径
                child.setMediaUrls(media.getUrl());
                // 设置文件时间
                child.setDuration(this.convertDuration(media.getDuration()));
            }
        }
        return result;
    }

    @Override
    public List<CourseDetail> getCourseDirectoryList(Long courseId) {
        return baseMapper.getCourseDirectoryList(courseId);
    }

    private String convertDuration(Long duration) {
        if (duration == null){
            return "";
        }
        long hours = TimeUnit.MILLISECONDS.toHours(duration);
        long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) % 60;
        long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) % 60;
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }
}
